package com.ruoyi.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;

import com.ruoyi.common.result.Result;

import com.ruoyi.exam.service.IMessageService;
import com.ruoyi.exam.domain.Message;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author FSD
 * @since 2024-02-20
 */
@Api(tags = "消息表")
@RestController
@RequestMapping("/api/system/message")
    public class MessageController {
@Resource(name = "MessageServiceImpl")
private IMessageService messageService;

        private String prefix = "/api/system/message";
        @RequiresPermissions("system:message:view")
        @GetMapping()
        public String message()
        {
                return prefix + "/message";
        }
/**
* 新增
*/
@PostMapping("/save")
@ApiOperation(value = "新增",notes = "新增")
@ApiImplicitParam(name = "message" ,value = "message实体类",dataTypeClass = String.class)
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result insert(@RequestBody Message message) {
        return Result.ok(messageService.save(message));
        }

/**
* 修改
*/
@PostMapping("/update")
@ApiOperation(value = "修改",notes = "修改")
@ApiImplicitParam(name = "message" ,value = "message实体类",dataTypeClass = String.class)
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result update(@RequestBody Message message) {
        return Result.ok(messageService.updateById(message));
        }

/**
* 根据id删除
*/
@PostMapping("/delete/{id}")
@ApiOperation(value = "根据id删除",notes = "根据id删除")
@ApiImplicitParam(name = "id" ,value = "id",dataTypeClass = Integer.class)
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result delete(@PathVariable Integer id) {
        return Result.ok(messageService.removeById(id));
        }


/**
* 查询全部
*/
@GetMapping("/getAll")
@ApiOperation(value = "查询全部",notes = "查询全部")
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result findAll() {
        return Result.ok(messageService.list());
        }

/**
* 根据id查询一个
*/
@GetMapping("getById/{id}")
@ApiOperation(value = "根据id查询一个",notes = "根据id查询一个")
@ApiImplicitParam(name = "id" ,value = "id",dataTypeClass = Integer.class)
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result findOne(@PathVariable Integer id){
        return Result.ok(messageService.getById(id));
        }

/**
* 分页模糊查询
*/
@GetMapping("/get/page")
@ApiOperation(value = "分页模糊查询",notes = "分页模糊查询")
@RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
public Result findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(messageService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
        }

